#include <my_header.h>
#include "directory.h"

int initPath(path_t *path)
{   

    path->absolutePath = (char*)calloc(1,1024);
    path->initialPath = (char*)calloc(1,1024);
    path->pwd = (char*)calloc(1,1024);

    pathStackInit(&path->pathStack);
    return 0;
}

int cd(path_t* path,char pathname[12][32],int len)
{
    /* char curPathName[][32] = pathname; */
    int i=0;
    if(pathname[0][0] == '/')
    {
        ++i;
        /* ++curPathName; */
        pathStackClear(&path->pathStack);
    }
    while(i<len )
    {
        if(strcmp(pathname[i],".")==0)
        {
            ;
        }
        else if(strcmp(pathname[i],"..")==0)
        {
            pathStackPop(&path->pathStack);
        }
        else
        {
            pathStackPush(&path->pathStack,pathname[i]);
        }
        ++i;
        /* ++curPathName; */
    } 
    
    bzero(path->pwd,1024);
    char* cur_p = path->pwd;
    sNode_t *cur = path->pathStack.bottom;
    //写入客户视角的绝对路径
    for(i = 0;i < path->pathStack.stackNum; ++i)
    {   
        sprintf(cur_p,"%s%s","/",cur->pathname);
        int len = strlen(cur->pathname)+1; 

        cur_p += len;
        cur = cur->next;
    }
    //写入客户在服务端的绝对路径
    sprintf(path->absolutePath,"%s%s",path->initialPath,path->pwd);
    if(path->pathStack.stackNum == 0)
    {
        printf("客户的pwd:%s\n","/");
        /* path->pwd[0] = '/'; */
    }
    else
    {
        printf("客户的pwd:%s\n",path->pwd);
    }

    /* sprintf(path->absolutePath,"%s%s",path->initialPath,path->pwd); */
    printf("服务端的pwd:%s\n",path->absolutePath);

    return 0;
}

int freePath(path_t *path)
{
    free(path->absolutePath);
    free(path->initialPath);
    free(path->pwd);
    pathStackClear(&path->pathStack);
    return 0;
}

int pwd(path_t *path,char* buf)
{
    if(path->pathStack.stackNum == 0)
    {
        buf[0] = '/';
    }
    else
    {
        sprintf(buf,"%s",path->pwd);
    }
    return 0;
}

int printPath(path_t *path)
{
    printPathStack(&path->pathStack);
    return 0;
}


/* typedef struct array_s */
/* { */
/*     char* name[100]; */
/*     int size; */
/* }array_t; */

#if 0
void test()
{
    char pathname[1024]={0};
    fgets(pathname,1024,stdin);
    puts(pathname);

    pathname[strlen(pathname)-1] = '\0';

    path_t path;
    initPath(&path);

    array_t array;
    bzero(&array,sizeof(array_t));

    char *p = strtok(pathname," /");

    /* printf("%s\n",p); */

    int i = 0;
    if(pathname[0] == '/')
    {
        array.name[0] = (char*)malloc(2);
        strcpy(array.name[0],"/");
        ++i;
        ++array.size;
    }
    array.name[i] = (char*)malloc(strlen(p)+1);
    strcpy(array.name[i],p);
    ++i;
    ++array.size;

    while(1)
    {
        p = strtok(NULL," /");
        if(p== NULL)
        {
            break;
        }
        /* printf("%s\n",p); */
        array.name[i] = (char*)malloc(strlen(p)+1);
        strcpy(array.name[i],p);
        ++i;
        ++array.size;
    }
    printf("------------------\n");
    cd(&path,array.name,array.size);
    freePath(&path);
    for(int index = 0;index<array.size;++index)
    {
        free(array.name[index]);
    }
}
#endif

#if 0
int main()
{
    test();
    printf("-------------------\n");
    
    path_t path;
    initPath(&path);

    char arr[] = "/home/joker/c++67/Client";

    char buf[1024];

    sprintf(path.initialPath,"%s",arr);

    char* name[] = {"/","how","are","you"};
    cd(&path,name,sizeof(name)/sizeof(name[0]));

    bzero(buf,sizeof(buf));
    pwd(&path,buf);
    printf("pwd = %s\n",buf);

    char* name2[] = {"..","..","..","..",".."};
    cd(&path,name2,sizeof(name2)/sizeof(name2[0]));

    bzero(buf,sizeof(buf));
    pwd(&path,buf);
    printf("pwd = %s\n",buf);

    char* name3[] = {"hello","world",".","c++","67"};
    cd(&path,name3,sizeof(name3)/sizeof(name3[0]));

    bzero(buf,sizeof(buf));
    pwd(&path,buf);
    printf("pwd = %s\n",buf);

    freePath(&path);
    
    return 0;
}
#endif
